/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import za.org.foobar.famon.util.DB;

import javax.jdo.annotations.*;
import java.util.Date;
import java.util.List;

/**
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Failure extends ModelObject {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private Key applicationGroupId;
    @NotPersistent
    private ApplicationGroup applicationGroup;

    // The machine which sent this failure
    @Persistent
    private String sourceHostname;
    @Persistent
    private String sourceAddress;
    @Persistent
    private int sourcePort;
    // The original machine which sent generated this failure
    @Persistent
    private String originalSourceHostname;
    @Persistent
    private String originalSourceAddress;
    // The user which was affected by the failure
    @Persistent
    private String user;
    // The type of the failure
    @Persistent
    private String failureType;
    // The failure's actual message
    @Persistent
    private String message;
    // The date and time the failure occurred
    @Persistent
    private Date date;

    @Persistent(mappedBy = "failure")
    @Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="name asc"))
    private List<ThreadInformation> threadList;

    public Failure() {
    }

    public Failure(Key applicationGroupId, String user, String failureType, String message, Date date) {
        this.applicationGroupId = applicationGroupId;
        this.user = user;
        this.failureType = failureType;
        this.message = message;
        this.date = date;
    }

    public Key getId() {
        return id;
    }

    public Key getApplicationGroupId() {
        return applicationGroupId;
    }

    public void setApplicationGroupId(Key applicationGroupId) {
        this.applicationGroupId = applicationGroupId;
    }

    public ApplicationGroup getApplicationGroup() {
        if (applicationGroup == null) {
            if (applicationGroupId == null) {
                return null;
            }
            applicationGroup = DB.findEx(ApplicationGroup.class, applicationGroupId);
        }
        return applicationGroup;
    }

    public String getSourceHostname() {
        return sourceHostname;
    }

    public void setSourceHostname(String sourceHostname) {
        this.sourceHostname = sourceHostname;
    }

    public String getSourceAddress() {
        return sourceAddress;
    }

    public void setSourceAddress(String sourceAddress) {
        this.sourceAddress = sourceAddress;
    }

    public int getSourcePort() {
        return sourcePort;
    }

    public void setSourcePort(int sourcePort) {
        this.sourcePort = sourcePort;
    }

    public String getOriginalSourceHostname() {
        return originalSourceHostname;
    }

    public void setOriginalSourceHostname(String originalSourceHostname) {
        this.originalSourceHostname = originalSourceHostname;
    }

    public String getOriginalSourceAddress() {
        return originalSourceAddress;
    }

    public void setOriginalSourceAddress(String originalSourceAddress) {
        this.originalSourceAddress = originalSourceAddress;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getFailureType() {
        return failureType;
    }

    public void setFailureType(String failureType) {
        this.failureType = failureType;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void addThread(ThreadInformation thread) {
        threadList.add(thread);
    }
    
    public List<ThreadInformation> getThreadList() {
        return threadList;
    }

    public void setThreadList(List<ThreadInformation> threadList) {
        this.threadList = threadList;
    }

    public void delete() {
        //Delete all threads
        deleteAll(threadList);
        //Delete self
        super.delete();
    }

    public String keyToString() {
        return keyToString(id);
    }

    public static String keyToString(Key id) {
        return Long.toString(id.getId());
    }

    public static Key stringToKey(String id) {
        return KeyFactory.createKey(Failure.class.getSimpleName(), Long.parseLong(id));
    }
}
